package com.leeoohoo.utils;

import com.leeoohoo.dto.UserStateDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class UserStateUtils {

    /**
     * 0000 0001(1)     -> REJECT                 -> 曾经有过 REJECT 状态，重新计算并且审批通过后，消除该状态
     * 0000 0010(2)     -> A1 Pre-check           -> A1 部门 pre-check不通过
     * 0000 0100(4)     -> Controlling Pre-check  -> Controlling 部门 pre-check不通过
     * 0000 1000(8)     -> A1 Pre-check           -> A1 部门 pre-check通过
     * 0001 0000(16)    -> Controlling Pre-check  -> Controlling 部门 pre-check通过
     * <p>
     * 0010 0000(32)    -> evaluation DRAFT       -> evaluation draft
     * 0100 0000(64)    -> evaluation CONFIRM     -> evaluation confirm
     * <p>
     * 1000 0000(128)               -> submit for approval    -> 提交到审批流的标识(未打包)
     * 0000 0001 0000 0000(256)     -> submit for approval    -> 提交到审批流的标识(已打包/未审批)
     * 0000 0010 0000 0000(512)     -> submit for approval    -> 提交到审批流的标识(审批中/已审批)
     * <p>
     * 0000 0100 0000 0000(1024)    -> split                  -> 拆分中
     */
    public static final Long ZERO = 0B0000_0000L;
    // 1
    public static final Long ACCOUNT_NON_EXPIRED = 0B0000_0001L;
    // 2
    public static final Long ACCOUNT_NON_LOCKED = 0B0000_0010L;
    // 4
    public static final Long CREDENTIALS_NON_EXPIRED = 0B0000_0100L;
    // 8
    public static final Long ENABLED = 0B0000_1000L;
    // 16
    public static final Long ACCOUNT_EXPIRED = 0B0001_0000L;
    // 32
    public static final Long ACCOUNT_LOCKED = 0B0010_0000L;
    // 64
    public static final Long CREDENTIALS_EXPIRED = 0B0100_0000L;
    // 128
    public static final Long NON_ENABLED = 0B1000_0000L;


    /**
     * statusFlag 转换成 AfsStatusFlagEntity
     *
     * @param statusFlag
     * @return
     */
    public static UserStateDTO toUserStateDTO(Long statusFlag) {
        if (Objects.isNull(statusFlag)) {
            return UserStateDTO.builder().build();
        }


        Boolean accountNonExpired = (ACCOUNT_NON_EXPIRED & statusFlag) == ACCOUNT_NON_EXPIRED;
        Boolean accountNonLocked = (ACCOUNT_NON_LOCKED & statusFlag) == ACCOUNT_NON_LOCKED;
        Boolean credentialsNonExpired = (CREDENTIALS_NON_EXPIRED & statusFlag) == CREDENTIALS_NON_EXPIRED;
        Boolean enabled = (ENABLED & statusFlag) == ENABLED;


        return UserStateDTO.builder().
                accountNonExpired(accountNonExpired).accountNonLocked(accountNonLocked).credentialsNonExpired(credentialsNonExpired)
                .enabled(enabled).build();
    }


    public static Long toState(UserStateDTO userStateDTO) {

        Boolean accountNonExpired = userStateDTO.getAccountNonExpired();
        Boolean accountNonLocked = userStateDTO.getAccountNonLocked();
        Boolean credentialsNonExpired = userStateDTO.getCredentialsNonExpired();
        Boolean enabled = userStateDTO.getEnabled();

        // reject 标签
        Long accountNonExpiredLong;
        if (Boolean.TRUE.equals(accountNonExpired)) {
            accountNonExpiredLong = ACCOUNT_NON_EXPIRED;
        } else {
            accountNonExpiredLong = ACCOUNT_EXPIRED;
        }

        //A1 操作标识
        Long accountNonLockedLong;
        if (Boolean.TRUE.equals(accountNonLocked)) {
            accountNonLockedLong = ACCOUNT_NON_LOCKED;
        } else {
            accountNonLockedLong = ACCOUNT_LOCKED;
        }

        //controlling 操作标识
        Long credentialsNonExpiredLong;
        if (Boolean.TRUE.equals(credentialsNonExpired)) {
            credentialsNonExpiredLong = CREDENTIALS_NON_EXPIRED;
        } else {
            credentialsNonExpiredLong = CREDENTIALS_EXPIRED;
        }

        //controlling 操作标识
        Long enabledLong;
        if (Boolean.TRUE.equals(enabled)) {
            enabledLong = ENABLED;
        } else {
            enabledLong = NON_ENABLED;
        }


        log.info("检验BreakHelper空值异常,rejectLong:{},aPreCheckLong:{},cPreCheckLong:{},pendingValue:{}",
                accountNonExpiredLong,
                accountNonLockedLong,
                credentialsNonExpiredLong,
                enabledLong);
        log.info("检验BreakHelper空值异常,afsStatusFlagEntity:{}", userStateDTO);
        return accountNonExpiredLong | accountNonLockedLong | credentialsNonExpiredLong | enabledLong;
    }


    public static void main(String[] args) {
        UserStateDTO build = UserStateDTO.builder()
                .accountNonLocked(true)
                .accountNonExpired(true)
                .credentialsNonExpired(true)
                .enabled(true)
                .build();
        Long aLong = toState(build);
        System.out.println(aLong);
        UserStateDTO userStateDTO = toUserStateDTO(aLong);
        System.out.println(userStateDTO.toString());


    }

}
